package com.yanqu.road.dataHelper.repair.logic;

import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.dataHelper.repair.mgr.UUIDHelper;
import com.yanqu.road.entity.player.UserMail;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/8/19
 */
public class VipBuChang extends BaseRepair {

    public VipBuChang(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {
        Date date = new Date();
        Map<Long, Integer> dataMap = getData();
        List<UserMail> userMails = new ArrayList<>();
        for (Map.Entry<Long, Integer> entry : dataMap.entrySet()) {
            UserMail userMail = new UserMail();
            userMail.setMailUid(UUIDHelper.saveRandomUUID(getGameServer().getServerInfo().getServerId() + ""));
            userMail.setMailType(0);
            userMail.setTitle("數值調整VIP補償");
            userMail.setContent("親愛的掌櫃：\n" +
                    "    本次版本更新涉及較多改動，很抱歉給掌櫃帶來許多不好的影響和體驗，為此，小二特地為您準備了豐厚的大禮，將為您發放以您在更新前的VIP經驗除以5的數量的抽獎券，還請掌櫃笑納，如果掌櫃對本次調整有任何疑問，可聯繫官網客服為您解答，祝掌櫃財運亨通，蒸蒸日上！");
            userMail.setAnnexes("110062=" + entry.getValue());
            userMail.setSendTime(date);
            userMail.setSendUserId(0);
            userMail.setSendNickName("系统管理员");
            userMail.setReceiveUserId(Long.valueOf(entry.getKey()));
            userMail.setRead(false);
            userMail.setReadTime(date);
            userMail.setGetReward(false);
            userMail.setDelete(false);
            userMails.add(userMail);
        }
        insertMail(userMails);
    }

    public Map<Long, Integer> getData() {
        Map<Long, Integer> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            call = getGameServer().getConnection().prepareCall("select userId, vipExp from t_u_user where vipExp > 0");
            rs = call.executeQuery();
            while (rs.next()) {
                long userId = rs.getLong("userId");
                int vipExp = rs.getInt("vipExp");

                int num = vipExp / 5;
                if (vipExp % 5 > 0) {
                    num += 1;
                }
                getLogger().error("{}， {}", userId, num);
                map.put(userId, num);
            }
        } catch (SQLException e) {
            getGameServer().getLogger().error("异常错误", e);
        } finally {
            getGameServer().closeConnection(call, rs);
            return map;
        }
    }

    public void insertMail(List<UserMail> userMails) {
        String sql = "INSERT INTO t_u_mail(mailUid,mailType,title,content,annexes,sendTime,sendUserId,sendNickName,receiveUserId,isRead,readTime,isGetReward,isDelete) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)";

        List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
        try {
            for (UserMail info : userMails) {
                Map<Integer, DbParameter> para = new HashMap<>();
                int i = 1;
                para.put(i++, new DbParameter(Types.NVARCHAR, info.getMailUid()));
                para.put(i++, new DbParameter(Types.INTEGER, info.getMailType()));
                para.put(i++, new DbParameter(Types.NVARCHAR, info.getTitle()));
                para.put(i++, new DbParameter(Types.NVARCHAR, info.getContent()));
                para.put(i++, new DbParameter(Types.VARCHAR, info.getAnnexes()));

                para.put(i++, new DbParameter(Types.TIMESTAMP, info.getSendTime()));
                para.put(i++, new DbParameter(Types.BIGINT, info.getSendUserId()));
                para.put(i++, new DbParameter(Types.VARCHAR, info.getSendNickName()));
                para.put(i++, new DbParameter(Types.BIGINT, info.getReceiveUserId()));

                para.put(i++, new DbParameter(Types.BOOLEAN, info.isRead()));
                para.put(i++, new DbParameter(Types.TIMESTAMP, info.getReadTime()));
                para.put(i++, new DbParameter(Types.BOOLEAN, info.isGetReward()));
                para.put(i++, new DbParameter(Types.BOOLEAN, info.isDelete()));
                mapList.add(para);
            }

            getGameServer().executeBatchQuery(sql, mapList);
        } catch (Exception e) {
            getLogger().error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
    }
}
